<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/



class edit_field  {

	var $title = FALSE;
	var $name = 'edit_field';
	var $entry = FALSE;

	var $id = FALSE;
	var $db_file;

	var $tag = FALSE;
	var $dname = FALSE;
	var $unit = FALSE;
	var $description = FALSE;

	function __construct($title="Edit Data Field",$name=FALSE) {
		$this->title = $title;	
		if ( $name ) $this->name = $name;
	}


	function edit($id,$db_file=FALSE) {

		// $id == FALSE add new record

		$this->id = $id;
		$this->db_file = $db_file;
		if ( ! $db_file ) $this->db_file = platform::app_data('Tazu-Timesheet-Manager').'config.db';


//		$cnf = new conf_timesheet(INIFILE);

		$glade = new GladeXML(PATH_RES.'edit_field.glade','edit_field');
		$glade->signal_autoconnect_instance($this);

		$dialog = $glade->get_widget('edit_field');
		$dialog->set_title($this->title);


/*
		$labels = array('name_lbl','company_lbl','email_lbl');
		foreach ( $labels as $lbl ) {
			$w = $glade->get_widget($lbl);
			$w->set_label(settings::xlate($w->get_label()));
		}		
*/


		$this->tag = $glade->get_widget('tag');
		$this->dname = $glade->get_widget('name');
		$this->unit = $glade->get_widget('unit');
		$this->description = $glade->get_widget('description');


		$tag = '';
		$name = '';
		$unit = '';
		$description = '';

		// Edit an existing customer record
		if ( $this->id ) {

			$c = new settings($this->db_file);
			
			$tag = $c->getDatTag($this->id);
			$name = $c->getDatName($this->id);
			$unit = $c->getDatUnit($this->id);
			$description = $c->getDatDescription($this->id);
			unset($c);
		}
			
		$this->tag->set_text($tag);
		$this->dname->set_text($name);
		$this->unit->set_text($unit);
		$this->description->set_text($description);

        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[1]; // get the ID of the OK button

        // simulate button click when user press enter
/*
		$this->entry->set_text($default);
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);
*/



        $dialog->show_all();
		$ok = FALSE;
		while ( ! $ok ) {

        $result = $dialog->run();

		//echo "+ ".Gtk::RESPONSE_OK."\n";
		//echo "- ".Gtk::RESPONSE_CANCEL."\n";
		//echo "->[$result]\n";

		if ( $result == Gtk::RESPONSE_OK ) {

			$tag = $this->tag->get_text();
			$name = $this->dname->get_text();
			$unit = $this->unit->get_text();
			$description = $this->description->get_text();

//			if ( $this->id && $timesheet_file <> '' ) {
//				$ok = $this->validate($timesheet_file);
//			}
//			else {
				$ok = TRUE;
//			}
			
			if ( $ok ) {
					
				$c = new settings($this->db_file);

				if ( $this->id ) {
					// Existing record
					$c->updateDat($this->id,'tag',$tag);							
					$c->updateDat($this->id,'name',$name);							
					$c->updateDat($this->id,'unit',$unit);							
					$c->updateDat($this->id,'description',$description);							

				}
				else {
					// New record
					$id = $c->addDat($tag, $name, $unit, $description);
				}
						
				unset( $c );
			}


			$result = $id;

		}
		else {
			$ok = TRUE;
			$result = FALSE;		// cancel
		}

		}

        $dialog->destroy();

		return($result);

    }


	// simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }



}

?>